package main.java;

import java.util.Objects;

public class NumberStatistics {
    // 计算平均值：支持Integer、Double、Float等数值类型
    public static <T extends Number> double average(DataContainer<T> numbers) {
        // 处理空容器
        if (numbers == null || numbers.size() == 0) {
            throw new IllegalArgumentException("numbers container cannot be null or empty");
        }
        double sum = 0.0;
        for (int i = 0; i < numbers.size(); i++) {
            T number = numbers.get(i);
            Objects.requireNonNull(number, "container cannot contain null elements");
            // 将Number转为double累加（避免精度丢失）
            sum += number.doubleValue();
        }
        // 平均值 = 总和 / 元素个数
        return sum / numbers.size();
    }

    // 计算最大值：数值类型且可比较
    public static <T extends Number & Comparable<? super T>> T max(DataContainer<T> numbers) {
        if (numbers == null || numbers.size() == 0) {
            throw new IllegalArgumentException("numbers container cannot be null or empty");
        }
        T max = numbers.get(0);
        for (int i = 1; i < numbers.size(); i++) {
            T current = numbers.get(i);
            Objects.requireNonNull(current, "container cannot contain null elements");
            // 调用Comparable的compareTo方法比较大小
            if (current.compareTo(max) > 0) {
                max = current;
            }
        }
        return max;
    }
}